﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;

namespace Services
{
    public class SingletonUnityService
    {
        public static IUnityService Instance;

        public static void ActivateUnityService(IUnityContainer parmUnityContainer)
        {
            if (Instance == null)
            {
                Instance = new UnityService(parmUnityContainer);
            }
        }
    }

    public class UnityService:IUnityService
    {
        readonly IUnityContainer Container;

        public UnityService(IUnityContainer container)
        {
            this.Container = container;
        }

        public T GetByType<T>()
        {
           return this.Container.Resolve<T>();
        }

        public Type Get(Type type)
        {
            return this.Container.Resolve(type) as Type;
        }

        public T GetTypeByName<T>(string Name)
        {
            return this.Container.Resolve<T>(Name);
        }

        public void InjectInstance<T>(T Instance)
        {
            this.Container.RegisterInstance(typeof(T), Instance, new ExternallyControlledLifetimeManager());
        }

        public void InjectAsSingleton<T>(T Instance)
        {
            this.Container.RegisterInstance(typeof(T), Instance);
        }

        public void RegisterByInterface<Interface, Implementation>() where Implementation : Interface
        {
            this.Container.RegisterType<Interface, Implementation>();
        }

        public void RegisterByInterfaceWithName<Interface, Implementation>(string key) where Implementation : Interface
        {
            this.Container.RegisterType<Interface, Implementation>(key);
        }

        public void RegisterAsSingletonByInterface<Interface, Implementation>() where Implementation : Interface
        {
            this.Container.RegisterType<Interface, Implementation>(new ContainerControlledLifetimeManager());
        }
    }
}
